import axios from 'axios'
import store from '@/store'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000
})

service.interceptors.request.use(
  config => {
    const token = store.getters.token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 问题： 无论密码正确与否都返回200
service.interceptors.response.use(
  response => {
    const res = response.data
    const { success, message } = res
    if (success === true) {
      return res
    } else {
      return Promise.reject(message)
    }
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
